package com.likou;

public class Demo002_实现strStr {
    public static void main(String[] args) {
        System.out.println(strStr("hello","ll"));
    }
    public static int strStr(String haystack, String needle) {
        if(haystack.equals(needle) || needle.equals("") ){
            return 0;
        }

        for (int i = 0; i < haystack.length(); i++) {
            if(haystack.charAt(i) == needle.charAt(0) && haystack.length()-i >= needle.length()){
 /*               if (needle.length() == 1){
                    return i;
                }*/

                String substring = haystack.substring(i, haystack.length());
                if (needle.equals(substring)){
                    return i;
                }

            }
        }
        return -1;

       /* if(haystack.equals(needle) || needle.equals("") ){
            return 0;
        }
        for (int i = 0; i < haystack.length(); i++) {
            if(haystack.charAt(i) == needle.charAt(0) && haystack.length()-i >= needle.length()){
                int index = i;
                boolean flag = true;
                for (int j = 1; j < needle.length(); j++) {
                    if (!(haystack.charAt(index+j) == needle.charAt(j) )){
                        flag = false;
                    }
                }
                if (flag){
                    return i;
                }
            }
        }
        return -1;*/
    }


}
